package com.jeffry.redial;

import com.jeffry.redial.Utils.Constants;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class DialNumbersListActivity extends ListActivity {

    private static final int MENU_SELECTED_ALL = 0;
    private static final int MENU_SELECTED_CLREA = 1;
    private static final int MENU_REMOVE_NUM = 2;
    private static final int MENU_USE_CURREN_NUM = 3;

    private SharedPreferences mPrefs;
    private ArrayList<String> mNumbersList;
    private ArrayAdapter<String> mAdapter;

    private EditText mInputNumberTxt;
    private Button mAddBtn;
    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dial_numbers_list);
        mPrefs = PreferenceManager.getDefaultSharedPreferences(this);

        mInputNumberTxt = (EditText) findViewById(R.id.add_num_txt);
        mAddBtn = (Button) findViewById(R.id.add_num_btn);
        mAddBtn.setOnClickListener(mAddBtnClickListener);

        mNumbersList = Utils.getNumbersList(this);
        mAdapter = new ArrayAdapter<String>(this,
                R.layout.simple_list_item_multiple_choice, R.id.text1, mNumbersList);
        mListView = getListView();
        mListView.setAdapter(mAdapter);
        mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        registerForContextMenu(mListView);
    }

    private OnClickListener mAddBtnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            String str = mInputNumberTxt.getText().toString();
            if (TextUtils.isEmpty(str)) {
                Toast.makeText(DialNumbersListActivity.this,
                        "Please input you number", Toast.LENGTH_SHORT).show();
            } else {
                mAdapter.add(str);
                mInputNumberTxt.setText("");
            }
        }
    };

    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, MENU_SELECTED_ALL, 0, "SelectAll");
        menu.add(0, MENU_SELECTED_CLREA, 0, "Clear");
        menu.add(0, MENU_REMOVE_NUM, 0, "Remove");
        menu.add(0, MENU_USE_CURREN_NUM, 0, "UseCurrent");

        return super.onCreateOptionsMenu(menu);
    };

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case MENU_SELECTED_ALL:
                for(int i = 0; i < mListView.getCount(); i++) {
                    mListView.setItemChecked(i, true);
                }
                break;
            case MENU_SELECTED_CLREA:
                mListView.clearChoices();
                mAdapter.notifyDataSetChanged();
                break;
            case MENU_REMOVE_NUM:
                ArrayList<String> list = new ArrayList<String>();
                for (int i = 0; i < mListView.getCount(); i++) {
                    if (mListView.isItemChecked(i)) {
                        list.add(mListView.getItemAtPosition(i).toString());
                    }
                }
                mNumbersList.removeAll(list);
                mListView.clearChoices();
                mAdapter.notifyDataSetChanged();
                break;
            case MENU_USE_CURREN_NUM:
                Intent intent = new Intent();
                intent.putExtra(Constants.EXTRA_NUMBERS_LIST, mNumbersList);
                setResult(Activity.RESULT_OK, intent);
                finish();
                break;
            default:
                break;

        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent intent = new Intent();
        intent.putExtra(Constants.EXTRA_NUMBERS_LIST, mNumbersList);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Editor ed = mPrefs.edit();

        String str = "";
        if (mNumbersList.size() > 0) {
            for (String s : mNumbersList) {
                str = s + ";" + str;
            }
        }
        ed.putString(Utils.Constants.NUMBERS_LIST, str);
        ed.commit();
    }

}
